//路由
using System;
using System.Linq;
using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using WebApplication1.Models;
using WebApplication1.Models.Database;

[Route("api/[controller]/[action]")]
[ApiController]
public class AuthenticationController : ControllerBase
{
    //注入jwt服务
    private readonly IJWTService _jwtService;
    // 注入数据库上下文
    private readonly WebEnterpriseIIContext _db;
    private readonly IUserService _userService;
    /// <summary>
    /// 构造函数注入
    /// </summary>
    /// <param name="jwtService"></param>
    public AuthenticationController(IJWTService jwtService, WebEnterpriseIIContext db,IUserService userService)
    {
        _jwtService = jwtService;
        _db = db;
        //从构造函数，获取用户服务
        _userService = userService;
    }

    /// <summary>
    /// 创建token
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public string CreateToken(string name)
    {
        return _jwtService.CreateToken(name);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [Authorize]
    public string UserInfo()
    {
        //获取用户信息
        return Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Name).Value;
    }


    // 用户操作
    // 请求方法POST
    /// <summary>
    /// 用户注册
    /// </summary>
    [HttpPost]
    //AddUser方法
    //User为Models/Database/User.cs中的类
    //user为前端传递的参数
    public string AddUser(AddUserRequests user)
    {
        if (!ModelState.IsValid)
        {
            return "参数错误";
        }
        //判断数据库中是否存在该用户
        if (_userService.ExistUserByUserName(user.UserName))
        {
            return "用户已存在";
        }
        /*  // 生成随机数作为用户id
          Random random = new Random();
          var model = new User();
          model.UserName = user.UserName;
          model.Password = user.Password;
          model.Email = user.Email;
          // 数据库中的UserId是自动增长的，所以无需进行设置
          // model.UserId = random.Next(1, 999);
          model.Salt = 0;
          model.UserLevel = 1;
          model.CreateTime = DateTime.Now;
          _db.Users.Add(model);*/

        var userId = _userService.AddNewUser(user.UserName, user.Email, user.NickName, user.Password);
        // 判断是否添加成功
        if (userId > 0)
        {
            return "用户添加成功";
        }
        return "用户添加失败";
    }

    // 用户登录
    // 请求方法POST
    /// <summary>
    /// 用户登录
    /// </summary>
    [HttpPost]
    //Login方法
    //User为Models/Database/User.cs中的类
    //user为前端传递的参数
    public string Login(string username, string password)
    {
        // 查询用户信息
        /* var user = _db.Users.FirstOrDefault(x => x.UserName == username);*/
        var user = _userService.GetUserByUserName(username);
        if (user == null)
        {
            return "用户不存在";
        };
        if (user.Password != password)
        {
            return "密码错误";
        }
        // 判断数据库中是否存在该用户
        // if (_db.Users.Where(u => u.UserName == username).Count() == 0)
        // {
        //     return "用户不存在";
        // }
        // // 判断密码是否正确
        // if (_db.Users.Where(u => u.UserName == username).First().Password != password)
        // {
        //     return "密码错误";
        // }
        // var userinfo = _db.Users.Where(u => u.UserName == username);
        // 返回账号信息
        // return _jwtService.CreateToken(userinfo.First().UserName);
        return _jwtService.CreateToken(username);
    }
}